<?php /**/ ?><?php
// $Id: template.php,v 1.15.2.1.2.15 2008/12/23 03:40:02 designerbrent Exp $

/**
 * Uncomment the following line during development to automatically
 * flush the theme cache when you load the page. That way it will
 * always look for new tpl files.
 */
 //drupal_flush_all_caches();

 function kb_preprocess_page(&$vars) {
  global $user;
  $vars['path'] = base_path() . path_to_theme() .'/';
  $vars['user'] = $user;

  // Fixup the $head_title and $title vars to display better.
  $title = drupal_get_title();
  $headers = drupal_set_header();

  // wrap taxonomy listing pages in quotes and prefix with topic
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    //$title = t('Topic') .' &#8220;'. $title .'&#8221;';
  }
  // if this is a 403 and they aren't logged in, tell them they need to log in
  else if (strpos($headers, 'HTTP/1.1 403 Forbidden') && !$user->uid) {
    $title = t('Please login to continue');
  }
  $vars['title'] = $title;

  if (!drupal_is_front_page()) {
    $vars['head_title'] = $vars['head_title'];
	//$title .' | '. $vars['site_name'];
    if ($vars['site_slogan'] != '') {
      $vars['head_title'] = $vars['head_title'];
	  //' &ndash; '. $vars['site_slogan'];
    }
  }

  // determine layout
  // 3 columns
  if ($vars['layout'] == 'both') {
    $vars['left_classes'] = 'col-left span-6';
    $vars['right_classes'] = 'col-right span-6 last';
    $vars['center_classes'] = 'col-center span-12';
    $vars['body_classes'] .= ' col-3 ';
  }
  // 2 columns
  else if ($vars['layout'] != 'none') {
    // left column & center
    if ($vars['layout'] == 'left') {
      $vars['left_classes'] = 'col-left span-6';
      $vars['center_classes'] = 'col-center span-18 last';
    }
    // right column & center
    else if ($vars['layout'] == 'right') {
      $vars['right_classes'] = 'col-right span-6 last';
      $vars['center_classes'] = 'col-center span-18';
    }
    $vars['body_classes'] .= ' col-2 ';
  }
  // 1 column
  else {

    $vars['center_classes'] = 'col-center span-24';
    $vars['body_classes'] .= ' col-1 ';
  }

  $vars['meta'] = '';
  // SEO optimization, add in the node's teaser, or if on the homepage, the mission statement
  // as a description of the page that appears in search engines
  if ($vars['is_front'] && $vars['mission'] != '') {
    $vars['meta'] .= '<meta name="description" content="'. blueprint_trim_text($vars['mission']) .'" />'."\n";
  }
  else if (isset($vars['node']->teaser) && $vars['node']->teaser != '') {
    $vars['meta'] .= '<meta name="description" content="'. blueprint_trim_text($vars['node']->teaser) .'" />'."\n";
  }
  else if (isset($vars['node']->body) && $vars['node']->body != '') {
    $vars['meta'] .= '<meta name="description" content="'. blueprint_trim_text($vars['node']->body) .'" />'."\n";
  }
  // SEO optimization, if the node has tags, use these as keywords for the page
  if (isset($vars['node']->taxonomy)) {
    $keywords = array();
    foreach ($vars['node']->taxonomy as $term) {
      $keywords[] = $term->name;
    }
    $vars['meta'] .= '<meta name="keywords" content="'. implode(',', $keywords) .'" />'."\n";
  }

  // SEO optimization, avoid duplicate titles in search indexes for pager pages
  if (isset($_GET['page']) || isset($_GET['sort'])) {
    $vars['meta'] .= '<meta name="robots" content="noindex,follow" />'. "\n";
  }

  /* I like to embed the Google search in various places, uncomment to make use of this
  // setup search for custom placement
  $search = module_invoke('google_cse', 'block', 'view', '0');
  $vars['search'] = $search['content'];
  */
  
  /* to remove specific CSS files from modules use this trick
  // Remove stylesheets
  $css = $vars['css'];
  unset($css['all']['module']['sites/all/modules/contrib/plus1/plus1.css']);
  $vars['styles'] = drupal_get_css($css);   
  */
}


 
 //Previous and Next links for image nodes
 function pn_node($node, $mode) {
  $output = NULL;

 $n_nid = $node->nid;
switch($mode) {
case 'p':
$n_nid--;
$op = '<';
$order = 'DESC';
$extra = '&laquo; &laquo;';
break;
case 'n':
$n_nid++;
$op = '>';
$order = 'ASC';
$extra = '&raquo; &raquo;';
break;
default:
return NULL;
}
/*
  switch($mode) {
    case 'p':
      $op = '<';
      $order = 'DESC';
      break;
    case 'n':
      $op = '>';
      $order = 'ASC';
      break;
    default:
      return NULL;
  }
*/
  $sql = "SELECT nid FROM {node} n
    WHERE nid $op %d
    AND type = 'image'
    AND status = 1
    AND promote = 1
    ORDER BY nid $order
    LIMIT 1";
  $o_nid = db_result(db_query($sql, $node->nid));
  if ($o_nid) {
    $o_node = node_load($o_nid);
    $output = '<a href="/'.$o_node->path.'#top">'.image_display($o_node, 'prevnext').'</a><br /><a class="nextprevlink" href="/'.$o_node->path.'#top">'.$extra.'</a>';
  }

  return $output;
}



function kb_preprocess_search_block_form(&$vars, $hook) {

  // Modify elements of the search form
  $vars['form']['search_block_form']['#title'] = '';

  // Set a default value for the search box
  $vars['form']['search_block_form']['#value'] = t('Search kBasarab.com');

  $vars['form']['search_block_form']['#attributes'] = array('onfocus' => 'this.value=\'\'', 'class' => t('cleardefault'));
  // Add a custom class to the search box

  // Change the text on the submit button
  $vars['form']['submit']['#value'] = 'Search';

  // Rebuild the rendered version (search form only, rest remains unchanged)
  unset($vars['form']['search_block_form']['#printed']);
  $vars['search']['search_block_form'] = drupal_render($vars['form']['search_block_form']);

  // Rebuild the rendered version (submit button, rest remains unchanged)
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);

  // Collect all form elements to make it easier to print the whole form.
  $vars['search_form'] = implode($vars['search']);
  
  //return $vars; 
}

function kb_aggregator_block_item($item, $feed = 0) {
  global $user;

  $output = '';
  if ($user->uid && module_exists('blog') && user_access('create blog entries')) {
    if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog it'))) {
      $output .= '<div class="icon">'. l($image, 'node/add/blog', array('attributes' => array('title' => t('Comment on this news item in your personal blog.'), 'class' => 'blog-it'), 'query' => "iid=$item->iid", 'html' => TRUE)) .'</div>';
    }
  }

  $feeditems = aggregator_feed_load($item->fid);
  
  // Display the external link to the item.
  //$output .= 'TESTING';
  $output .= '<div class="feedname">'.$feeditems[title].'</div><div class="feedtitle"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></div>\n";

  return $output;
}
